class User < ActiveRecord::Base
  #attr_accessible :name, :oauth_expires_at, :oauth_token, :provider, :uid
  def self.from_omniauth(auth)
    where(auth.slice(:provider, :uid)).first_or_initialize.tap do |user|
      user.provider = auth.provider
      user.uid = auth.uid
      user.name = auth.info.name
      user.oauth_token = auth.credentials.token
      user.oauth_expires_at = Time.at(auth.credentials.expires_at)
      user.save!
    end
  end
  
  def facebook
    @facebook ||= Koala::Facebook::API.new(oauth_token)
    block_given? ? yield(@facebook) : @facebook
  rescue Koala::Facebook::APIError => e
    logger.info e.to_s
    nil # or consider a custom null object
  end

  def friend_picture
      #f = facebook { |fb| fb.get_connection("me", "friends")}
      
      # kyle's pic || facebook { |fb| fb.get_picture(1193647565)}
      
      s = facebook { |fb| fb.get_connection("me", "friends").last}
      value = s["id"]       
      facebook { |fb| fb.get_picture(value)}
  end
  
  
  def friend_count
    friend = facebook { |fb| fb.get_connection("me", "friends").count}
  end
  
  def me_facebook
    pictures = facebook { |fb| fb.get_picture("me")}
    #friends = facebook.get_connections(profile["id"], "friends")
  end
end
